package com.jevons.geo_edit.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.jevons.geo_edit.R;
import com.jevons.geo_edit.adapter.LayerManagerAdapter;
import com.jevons.geo_edit.constant.GisGeometryType;
import com.jevons.geo_edit.databinding.FragmentGisMapLayerManangerBinding;
import com.jevons.geo_edit.dialog.CreateGisLayerDialog;
import com.jevons.geo_edit.event.GisMapLayerOpEvent;
import com.jevons.geo_edit.model.GisField;
import com.jevons.geo_edit.model.GisLayer;
import com.jevons.muffin.fragment.BaseBindingFragment;
import com.jevons.muffin.util.UuidUtils;
import com.jevons.muffin.utilcode.StringUtils;
import com.jevons.muffin.vm.BasePresenter;
import com.jevons.muffin.widget.RecycleViewDivider;
import com.jevons.muffin.widget.dialog.MainSimpleDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * Copyright 2020 JxGIS
 *
 * @file GisLayerManagerFragment
 * @auth linaisheng
 * Created on 2023/6/9.
 * Description：
 */
public class GisLayerManagerFragment extends BaseBindingFragment<FragmentGisMapLayerManangerBinding, BasePresenter> {

    private String projectId;
    private List<GisLayer> gisLayerList = new ArrayList<>();
    private LayerManagerAdapter layerAdapter;
    private String selectedLayerId;

    @Override
    public int getContentView() {
        return R.layout.fragment_gis_map_layer_mananger;
    }

    @Override
    public void onInitView(Bundle savedInstanceState) {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        dataBinding.recyclerView.addItemDecoration(new RecycleViewDivider(
                getContext(), LinearLayoutManager.VERTICAL));
        layerAdapter = new LayerManagerAdapter(getContext(), new LayerManagerAdapter.OnItemOpListener() {
            @Override
            public void onClickItem(int position) {
                GisLayer gisLayer = layerAdapter.getItem(position);
                String selectedId = null;
                if (StringUtils.isNotEmpty(layerAdapter.getSelectedLayerId())) {
                    if (layerAdapter.getSelectedLayerId().equals(gisLayer.getId())) {
                        selectedId = null;
                    } else {
                        selectedId = gisLayer.getId();
                    }
                } else {
                    selectedId = gisLayer.getId();
                }
                if (selectedId == null) {
                    setSelectedLayer(null);
                } else {
                    setSelectedLayer(gisLayer);
                }

                EventBus.getDefault().post(new GisMapLayerOpEvent(
                        GisMapLayerOpEvent.EVENT_SELECTED_LAYER,
                        StringUtils.isNotEmpty(selectedId) ? gisLayer : null));
            }

            @Override
            public void onClickItemVisible(int position) {
                GisLayer gisLayer = layerAdapter.getItem(position);
                gisLayer.setLayerVisible(!gisLayer.isLayerVisible());
                layerAdapter.notifyItemChanged(position);
                EventBus.getDefault().post(new GisMapLayerOpEvent(
                        gisLayer.isLayerVisible() ?
                                GisMapLayerOpEvent.EVENT_NOTIFY_LAYER_VISIBLE :
                                GisMapLayerOpEvent.EVENT_NOTIFY_LAYER_INVISIBLE,
                        gisLayer));
            }

            @Override
            public void onClickItemGeometry(int position) {
                GisLayer gisLayer = layerAdapter.getItem(position);
                EventBus.getDefault().post(new GisMapLayerOpEvent(
                        GisMapLayerOpEvent.EVENT_SHOW_LAYER_GEOMETRY_LIST, gisLayer));
            }

            @Override
            public void onClickItemEdit(int position) {
                GisLayer gisLayer = layerAdapter.getItem(position);
                EventBus.getDefault().post(new GisMapLayerOpEvent(
                        GisMapLayerOpEvent.EVENT_SHOW_LAYER_SETTING, gisLayer));
            }

            @Override
            public void onClickItemEditField(int position) {
                GisLayer gisLayer = layerAdapter.getItem(position);
                EventBus.getDefault().post(new GisMapLayerOpEvent(
                        GisMapLayerOpEvent.EVENT_SHOW_LAYER_FIELD_LIST, gisLayer));
            }

            @Override
            public void onClickItemDelete(int position) {
                new MainSimpleDialog(getContext())
                        .setMessage("是否删除该图层？")
                        .setCancel("取消", null)
                        .setConfirm("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                GisLayer gisLayer = layerAdapter.getItem(position);
                                layerAdapter.remove(position);
                                EventBus.getDefault().post(new GisMapLayerOpEvent(
                                        GisMapLayerOpEvent.EVENT_NOTIFY_LAYER_REMOVE,
                                        gisLayer));
                            }
                        }).show();
            }


            @Override
            public void onItemMove(int fromPosition, int toPosition) {
                GisLayer gisLayer = layerAdapter.getItem(fromPosition);
                GisMapLayerOpEvent opEvent = new GisMapLayerOpEvent(GisMapLayerOpEvent.EVENT_NOTIFY_LAYER_SORT_CHANGE, gisLayer);
                opEvent.addData("fromPosition", Integer.toString(fromPosition));
                opEvent.addData("toPosition", Integer.toString(toPosition));
                EventBus.getDefault().post(opEvent);
            }
        });
        layerAdapter.bindToRecyclerView(dataBinding.recyclerView);


        if (getArguments().containsKey("projectId")) {
            projectId = getArguments().getString("projectId");
        }

        layerAdapter.setNewData(gisLayerList);

        if (getArguments().containsKey("selectedLayerId")) {
            selectedLayerId = getArguments().getString("selectedLayerId");
        }
        layerAdapter.setSelectedLayerId(StringUtils.isNotEmpty(
                selectedLayerId) ? selectedLayerId : null);


        dataBinding.tvCreateLayer.setOnClickListener((view) -> {
            new CreateGisLayerDialog(getContext(), new CreateGisLayerDialog.OnCreateLayerListener() {
                @Override
                public void onCreate(GisGeometryType geoType, String layerName) {
                    GisLayer layer = new GisLayer(projectId, geoType, layerName);
                    EventBus.getDefault().post(new GisMapLayerOpEvent(
                            GisMapLayerOpEvent.EVENT_CREATE_LAYER, layer));
                }
            }).show();
        });
    }


    public void initData(List<GisLayer> gisLayers) {
        gisLayerList.clear();
        for (GisLayer gisLayer : gisLayers) {
            gisLayerList.add(gisLayer);
        }
        if (layerAdapter != null) {
            layerAdapter.setNewData(gisLayerList);
        }
    }

    public void setSelectedLayer(GisLayer gisLayer) {
        if (gisLayer != null) {
            selectedLayerId = gisLayer.getId();
        } else {
            selectedLayerId = null;
        }
        if (layerAdapter != null) {
            layerAdapter.setSelectedLayerId(gisLayer != null ? gisLayer.getId() : null);
        }
    }

    public void notifyGisLayerChange(GisLayer gisLayer) {
        layerAdapter.notifyDataSetChanged();
    }

}